package fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.lanou.horo.myapplication.R;
import com.lanou.horo.myapplication.dbdao.HostDbEntity;
import com.lanou.horo.myapplication.dbdao.RecommendDbEntity;

import java.util.ArrayList;
import java.util.List;

import dbdao.HostDBHelper;
import dbdao.RecommendDBHelper;
import entity.HostPageEntity;
import entity.LiveFmEntity;
import fragment.tabhostFragment.PersonalDetailsFragment;
import tools.ItemOnClickListenerOther;
import utils.adapter.HostPageApter;
import volley.VolleySingleton;

/**
 * 主播 fragment
 * Created by dllo on 15/11/17.
 */
public class HostPageFragment extends AbsBaseFragment implements ItemOnClickListenerOther {
    private RecyclerView recyclerView;
    private HostPageApter adpter;
    private RecommendDBHelper helper;

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_page_host;
    }

    @Override
    protected void initView() {
        recyclerView = findView(R.id.hostRecylerView);
        GridLayoutManager m=new GridLayoutManager(getContext(),1);
        recyclerView.setLayoutManager(m);
        helper = RecommendDBHelper.getInstance();

    }

    @Override
    protected void initData() {
        adpter = new HostPageApter();
        recyclerView.setAdapter(adpter);
        adpter.setListenert(this);
        initNetwork();

    }
    String URL="http://mobile.ximalaya.com/m/explore_user_index?device=android&page=1";

    private void initNetwork() {
        VolleySingleton.getInstance().addRequest(URL,
                HostPageEntity.class, new Response.Listener<HostPageEntity>() {
                    @Override
                    public void onResponse(HostPageEntity response) {
                        if (response != null) {
                            List<HostPageEntity.ListEntity> result = response.getList();
                            adpter.addData(result);

                            dismissDialog();
                        }
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        dismissDialog();
                        toastError();
                    }
                });
        showDialog();
    }

    @Override
    public void itemListener(Object o, int i) {
        HostPageEntity.ListEntity list = (HostPageEntity.ListEntity) o;
        //点击关注的时候存数据
        List<RecommendDbEntity>datas = new ArrayList<>();
        RecommendDbEntity entity = new RecommendDbEntity();
        switch (i){
            case 0:
               Toast.makeText(getContext(), "已关注", Toast.LENGTH_SHORT).show();


                entity.setNickname(list.getList().get(0).getNickname());
                entity.setSmallLogo(list.getList().get(0).getSmallLogo());
                entity.setUid(list.getList().get(0).getUid());
                entity.setTitle(list.getTitle());
                datas.add(entity);
                helper.saveRecommend(datas);

                break;
            case 1:
                Toast.makeText(getContext(), "已关注", Toast.LENGTH_SHORT).show();
                entity.setNickname(list.getList().get(1).getNickname());
                entity.setSmallLogo(list.getList().get(1).getSmallLogo());
                entity.setUid(list.getList().get(1).getUid());
                entity.setTitle(list.getTitle());
                datas.add(entity);
                helper.saveRecommend(datas);
                break;
            case 2:
                Toast.makeText(getContext(), "已关注", Toast.LENGTH_SHORT).show();
                entity.setNickname(list.getName());
                entity.setSmallLogo(list.getList().get(2).getSmallLogo());
                entity.setUid(list.getList().get(2).getUid());
                entity.setTitle(list.getList().get(2).getNickname());
                datas.add(entity);
                helper.saveRecommend(datas);
                break;
            case 3:
                goToNext(new PersonalDetailsFragment(list.getList().get(0).getUid()));
                break;
            case 4:
                goToNext(new PersonalDetailsFragment(list.getList().get(1).getUid()));
                break;
            case 5:
                goToNext(new PersonalDetailsFragment(list.getList().get(2).getUid()));
                break;

        }
    }
}
